using System;
using UnityEngine;

namespace Scanline.Core
{
    internal class OrderedEdge : IComparable<OrderedEdge>
    {
        internal Vector2Int from;
        internal Vector2Int to;
        internal int yMin;
        internal int yMax;
        internal int sortingValue;

        private float currentX;
        internal float deltaX;

        internal OrderedEdge(Vector2Int a, Vector2Int b, int sortingValue)
        {
            if(a.y < b.y) 
            {
                from = a;
                to = b;
            }
            else
            {
                from = b;
                to = a;
            }
            yMin = from.y;
            yMax = to.y;
            this.sortingValue = sortingValue;
            if (yMax > yMin)
                deltaX = (to.x - from.x) / (float)(yMax - yMin);
            currentX = from.x - deltaX;
        }

        /// <summary>
        /// 计算下一个交点的x分量，并返回
        /// </summary>
        /// <returns></returns>
        internal int MoveUp()
        {
            currentX += deltaX;
            Debug.Log($"{currentX}——{ToString()}");
            return Mathf.RoundToInt(currentX);
        }

        public override string ToString()
        {
            return $"from:{from} to:{to} sortingValue:{sortingValue}";
        }

        public int CompareTo(OrderedEdge other)
        {
            return sortingValue - other.sortingValue;
        }
    }
}